package logic

import (
	"context"
	deptApi "mldong/api/dept"
	dao "mldong/internal/modules/sys/dao"
	"mldong/internal/modules/sys/entity"
	service "mldong/internal/modules/sys/service"
	"mldong/utility"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/jinzhu/copier"
)

func init() {
	// 注册服务实现
	utility.Register(service.DeptServiceType, NewDeptLogic())
}

type DeptLogic struct {
	deptDao *dao.DeptDao
}

// 保存部门接口实现
func (logic *DeptLogic) Save(ctx context.Context, req *deptApi.SaveReq) (res bool, err error) {
	unique := utility.CheckUnique(logic.deptDao.Model(nil), "code", req.Code, nil, "唯一编码已存在，请检查code参数")
	if unique != nil {
		return false, unique
	}
	var dept = entity.Dept{}
	err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
		req.ID = nil
		// 将req对象赋值给dept
		copier.Copy(&dept, &req)
		// 插入sys_dept表
		dept, err = logic.deptDao.Insert(dept, tx)
		if err != nil {
			return err
		}
		return nil
	})
	return dept.ID != nil, err
}

// 修改部门接口实现
func (logic *DeptLogic) Update(ctx context.Context, req *deptApi.UpdateReq) (res bool, err error) {
	unique := utility.CheckUnique(logic.deptDao.Model(nil), "code", req.Code, req.ID, "唯一编码已存在，请检查code参数")
	if unique != nil {
		return false, unique
	}
	var dept = entity.Dept{}
	var count int64
	err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
		// 将req对象赋值给dept
		copier.Copy(&dept, &req)
		count, err = logic.deptDao.UpdateById(dept, nil)
		return err
	})
	return count > 0, err
}

// 删除部门接口实现
func (logic *DeptLogic) Remove(ctx context.Context, ids []string) (res bool, _ error) {
	// 遍历ids
	// for _, id := range ids {
	// 	logic.deptDao.DeleteById(id)
	// }
	var count, err = logic.deptDao.DeleteByIds(ids)
	if err != nil {
		return false, err
	}
	if count == 0 {
		return false, err
	}
	return true, err
}

// 根据ID获取部门信息
func (logic *DeptLogic) Detail(ctx context.Context, id string) (res deptApi.DeptVO, err error) {
	var deptVO = deptApi.DeptVO{}
	var dept entity.Dept
	dept, err = logic.deptDao.GetById(id)

	// 将dept对象赋值给deptVO
	copier.Copy(&deptVO, &dept)

	return deptVO, err
}

// 分页查询部门接口实现
func (logic *DeptLogic) Page(ctx context.Context, req *deptApi.PageReq) (res deptApi.DeptPageVO, err error) {
	// 处理分页参数nil值
	utility.HandlePageParam(&req.PageParam)
	depts, total, err := logic.deptDao.Page(*req.PageNum, *req.PageSize, func(model *gdb.Model) *gdb.Model {
		model = utility.BuildQuery(req.PageParam, model)
		if req.OrderBy == "" {
			model = model.OrderAsc("t.sort")
		}
		return model
	})
	var deptVOS = []deptApi.DeptVO{}
	copier.Copy(&deptVOS, &depts)
	deptPage := deptApi.DeptPageVO{
		Rows: deptVOS,
	}
	deptPage.PageNum = *req.PageNum
	deptPage.PageSize = *req.PageSize
	deptPage.RecordCount = total
	// 计算 TotalPage
	if *req.PageSize > 0 {
		deptPage.TotalPage = (total + *req.PageSize - 1) / *req.PageSize
	} else {
		deptPage.TotalPage = 0 // 如果 PageSize 为 0，则 TotalPage 为 0
	}
	return deptPage, err
}

// 查询部门列表接口实现
func (logic *DeptLogic) List(ctx context.Context, req *deptApi.ListReq) (res []*deptApi.DeptVO, err error) {
	var deptVOS = []*deptApi.DeptVO{}
	depts, err := logic.deptDao.List(func(model *gdb.Model) *gdb.Model {
		model = utility.BuildQuery(req.PageParam, model)
		if req.OrderBy == "" {
			model = model.OrderAsc("t.sort")
		}
		return model
	})
	copier.Copy(&deptVOS, &depts)
	if err != nil {
		return nil, err
	}
	return deptVOS, err
}

// 查询部门树接口实现
func (logic *DeptLogic) Tree(ctx context.Context, req *deptApi.TreeReq) (res []*deptApi.DeptVO, err error) {
	listReq := deptApi.ListReq{}
	copier.Copy(&listReq, &req)
	res, err = logic.List(ctx, &listReq)
	if err != nil {
		return nil, err
	}

	// 使用泛型 TreeTool
	treeTool := &utility.TreeTool[*deptApi.DeptVO]{}
	treeNodes := treeTool.ListToTree(res, "0")

	return treeNodes, nil
}
func NewDeptLogic() service.IDeptService {
	return &DeptLogic{
		deptDao: dao.NewDeptDao(),
	}
}
